import Vue from 'vue'
import Vuex from 'vuex'


Vue.use(Vuex)
export default new Vuex.Store({
    state: {
        ShoppingCart:[],

        CommodityData: [],
        CommodityCount:'0',
        SearchCommodity:[],
        is_admin:0

    },
    mutations: {
        setData(state, CommodityData) {

            state.CommodityData = CommodityData

        },
        setSearchData(state, SearchCommodity) {

            state.SearchCommodity = SearchCommodity

        },
        delCommodity(state, index){
            console.log(typeof (index))
            state.CommodityData.splice(index,1)

        },
        setCommodityCount(state, CommodityCount) {

            state.CommodityCount = CommodityCount

        },

        setAdmin(state){
            state.is_admin=1
        },

        addShopping(state,shop_data){
            //添加数据到购物车列表
            state.ShoppingCart.push(shop_data)

        },

        delShopping(state,index){
            state.ShoppingCart.splice(index,1)
            console.log(state.ShoppingCart)

        }
    }
    ,getters:{
        CommodityCount(state){

            return state.CommodityCount
        }
    },
    actions: {

        getCommodityData (context,{page, pageSize}) {
            var _this = this
            axios.get("http://www.qjhyuki.top/vuehomeword/VueShop/api/select_commodity.php", {
                params: {
                    page: page,
                    pageSize: pageSize
                }
            }).then(({data}) => {

                context.commit('setData',data)

            })
        },
        delCommodityData(context,id){

        },
        getSearchCommodityData (context,{page, pageSize,title}) {
            var _this = this
            axios.get("http://www.qjhyuki.top/vuehomeword/VueShop/api/search_comodity.php", {
                params: {
                    page: page,
                    pageSize: pageSize,
                    title:title
                }
            }).then(({data}) => {

                context.commit('setSearchData',data)
                if(data.length===0){
                    alert('查无此货!');
                }

            })
        },
        getSearchShopNameData (context,{page, pageSize,Name}) {
            var _this = this
            axios.get("http://www.qjhyuki.top/vuehomeword/VueShop/api/search_ShopName.php", {
                params: {
                    page: page,
                    pageSize: pageSize,
                    ShopName:Name
                }
            }).then(({data}) => {

                context.commit('setSearchData',data)
                if(data.length===0){
                    alert('查无此货!');
                }

            })
        },
        getCommodityCount (context) {
            var _this = this
            axios.get("http://www.qjhyuki.top/vuehomeword/VueShop/api/get_commodity_count.php", {
            }).then(({data}) => {

                context.commit("setCommodityCount",data)

            })
        },

    }
})
